package com.atguigu.system.service.impl;

import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.system.SysUserRole;
import com.atguigu.model.vo.AssginRoleVo;
import com.atguigu.model.vo.RouterVo;
import com.atguigu.model.vo.SysUserQueryVo;
import com.atguigu.system.handler.GuiguException;
import com.atguigu.system.mapper.SysRoleMapper;
import com.atguigu.system.mapper.SysUserMapper;
import com.atguigu.system.mapper.SysUserRoleMapper;
import com.atguigu.system.service.SysMenuService;
import com.atguigu.system.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-12-12
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public IPage<SysUser> selectPage(Page<SysUser> pageParam, SysUserQueryVo sysUserQueryVo) {
        return baseMapper.selectPage(pageParam, sysUserQueryVo);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        SysUser sysUser = baseMapper.selectById(id);
        if (sysUser != null) {
            sysUser.setStatus(status);
            baseMapper.updateById(sysUser);
        } else {
            throw new GuiguException(ResultCodeEnum.DATA_ISNULL);
        }
        return true;
    }

    @Override
    public Map<String, Object> getRolesById(Long id) {
        Map<String, Object> map = new HashMap<>();
        //所有角色
        List<SysRole> roles = sysRoleMapper.selectList(null);
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        List<SysUserRole> userRoles = sysUserRoleMapper.selectList(wrapper);
        List<String> userRoleIds = new ArrayList<>();
        for (SysUserRole userRole : userRoles) {
            userRoleIds.add(userRole.getRoleId());
        }
        map.put("allRoles", roles);
        map.put("userRoleIds", userRoleIds);
        return map;
    }

    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        //删除之前分配角色
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", assginRoleVo.getUserId());
        sysUserRoleMapper.delete(wrapper);
        List<String> roleIdList = assginRoleVo.getRoleIdList();
        for (String roleId : roleIdList) {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(assginRoleVo.getUserId());
            userRole.setRoleId(roleId);
            sysUserRoleMapper.insert(userRole);
        }
    }

    @Override
    public SysUser getUserInfoByUserName(String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {
        SysUser user = this.getUserInfoByUserName(username);
        //根据userId查询菜单权限
        List<RouterVo> routerVos = sysMenuService.getUserMenuList(user.getId());
        //根据userId查询用户按钮权限
        List<String> permsList = sysMenuService.getUserButtonList(user.getId());
        Map<String, Object> result = new HashMap<>();
        result.put("name", username);
        result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        result.put("roles", "[admin]");
        //菜单权限数据
        result.put("routers", routerVos);
        //按钮权限数据
        result.put("buttons", permsList);
        return result;
    }
}
